package org.hcf.api.tencent.callback;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.hcf.api.base.Const;
import org.hcf.api.base.Helper;
import org.hcf.api.tencent.callback.info.group.GroupDestroyInfo;
import org.hcf.api.tencent.callback.info.group.base.MemberInfo;
import org.hcf.api.intf.ICallBack;
import org.hcf.common.util.TimeUtils;
import org.hcf.data.RedisUtils;
import org.hcf.data.SqlHelper;
import org.hcf.data.entity.ChatGroup;
import org.hcf.data.entity.ChatGroupInfo;
import org.hcf.data.service.IChatGroupInfoService;
import org.hcf.data.service.IChatGroupService;

import java.util.Set;

@Slf4j
/** 群解散回调 */
public class CallbackGroupDeleteHandler implements ICallBack {
    @Override
    public String getCommand() {
        return Const.CMD.GROUP_DESTROY_AFTER;// "Group.CallbackAfterGroupDestroyed";
    }
//        {
//            "CallbackCommand": "Group.CallbackAfterGroupDestroyed", // 回调命令
//                "Operator_Account": "administrator", //操作者
//                "GroupId" : "@TGS#2J4SZEAEL",
//                "Type": "Public", // 群组类型
//                "Owner_Account": "leckie", // 群主
//                "Name": "MyFirstGroup", // 群组名称
//                "MemberList" : [ //被解散的群组中的成员
//            {
//                "Member_Account": "leckie"
//            },
//            {
//                "Member_Account": "peter"
//            },
//            {
//                "Member_Account": "bob"
//            }
//    ],
//            "EventTime": 1670574414123//毫秒级别，事件触发时间戳
//        }

    @Override
    public JSONObject action(JSONObject data) {
        log.info("收到群组解散之后回调:" + data);
        GroupDestroyInfo info = data.toJavaObject(GroupDestroyInfo.class);
        log.info("反序列化群解散信息:" + JSONObject.toJSON(info));

        if (!getCommand().equals(info.getCallbackCommand())) {
            log.error("删群消息中[" + data.getString("CallbackCommand") + "]命令不匹配");
            return Helper.toSuccess();
        }

        ChatGroup group = RedisUtils.getGroupByAccount(info.getGroupId(), false);
        if (group != null) {
            group.setDispose(true);
            SqlHelper.getService(IChatGroupService.class).updateById(group);
            RedisUtils.putGroup(group);
            Set<Long> uids = Helper.getPluginUserIds();
            for (MemberInfo member : info.getMemberList()) {
                if (member != null && NumberUtil.isNumber(member.getMember_Account())) {
                    long userId = Long.parseLong(member.getMember_Account());
                    ChatGroupInfo memberInfo = RedisUtils.getGroupMember(userId, group.getGroupId());
                    if (memberInfo != null) {
                        memberInfo.setKeepGroup(false);
                        memberInfo.setModifyTime(TimeUtils.now());
                        RedisUtils.putMember(memberInfo);
                        SqlHelper.getService(IChatGroupInfoService.class).updateById(memberInfo);
                        RedisUtils.removeUID2GMID(userId, memberInfo.getInfoId());
                    }
                    if (uids.contains(userId)) {
                        RedisUtils.removeGA2UID(info.getGroupId(), userId);
                    }
                }
            }
        } else log.error("群不存在！！！无法回调消息:" + data);

        return Helper.toSuccess();
    }
}
